<template>
  <div class="clas_All">
    <el-form class="const_top" :model="queryParams" size="small" :inline="true">
      <el-row>
        <el-col :span="24">
          <el-form-item prop="" style="height: 40px">
            <el-select
              v-model="byMonth.relateId"
              placeholder="请选择部门"
              clearable
              @change="deptChange"
            >
              <el-option
                v-for="dict in deptOptions"
                :key="dict.relateId"
                :label="dict.label"
                :value="dict.relateId"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="" style="height: 40px">
            <el-date-picker
              clearable
              v-model="queryParams.createdTime"
              type="month"
              placeholder="选择月"
              @change="createdTimeChange"
              :disabled="mouday"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item prop="" style="height: 40px">
            <el-select
              v-model="queryParams.pandianCode"
              placeholder="1号至10号"
              clearable
              :disabled="mount"
            >
              <el-option
                v-for="dict in NewDropDownList"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <!-- <el-form-item prop="status" style="height: 40px">
            <el-select
              v-model="queryParams.createdBy"
              placeholder="指标维度"
              clearable
            >
               <el-option
                v-for="dict in producerOption"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              /> -->
          <!-- </el-select>
          </el-form-item> -->
          <el-button type="primary" size="mini" @click="getTop"
            >查询
          </el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-row>
      <el-col :span="24">
        <div ref="selectOne" style="width: 100%; height: 329px"></div>
        <div ref="selectTwo" style="width: 100%; height: 329px"></div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  getTargetManageDiscount,
  getTargetManageReal,
} from "@/api/system/cost.js";
import { getPanDianCode, FindShopBom } from "@/api/system/stock";
export default {
  data() {
    return {
      queryParams: {
        paramDate: "",
        paramMounth: "",
      },
      tProduct: [],
      xAxistProduct: [],
      ProductPositive: [],
      xAxisProduct: [],
      deptOptions: [],
      DropDownList: [],
      NewDropDownList: [],
      tableData: [],
      byMonth: {},
      mouday: true,
      mount: true,
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  mounted() {
    this.getTop();
    this.SleectOne();
    this.SleectTwo();
    FindShopBom().then((res) => {
      // console.log(res);
      if (res.code == 200) {
        this.deptOptions = res.data;
      }
    });
  },
  methods: {
    getTop() {
      let arr = [];
      if (this.queryParams.paramDate != "") {
        arr = this.queryParams.paramDate.split("-");
        this.queryParams.paramMounth = parseInt(arr[1], 10);
      }
      //折扣前
      getTargetManageDiscount(this.queryParams).then((res) => {
        if (res.code == 200) {
          res.data.forEach((item) => {
            this.tProduct.push(item.sumAmount);
            this.xAxistProduct.push(item.foodName);
          });
          this.SleectOne();
        }
      });
      // 实际毛利率
      getTargetManageReal(this.queryParams).then((res) => {
        if (res.code == 200) {
          res.data.forEach((item) => {
            this.ProductPositive.push(item.rate);
            // this.ProductPositive.push(item.dValue);
            this.xAxisProduct.push(item.createTime);
          });
          this.SleectTwo();
        }
      });
    },
    handleQuery() {},
    // 柱状图
    SleectOne() {
      let chartDom = this.$refs.selectOne;
      let myChart = this.$echarts.init(chartDom);
      let option;
      option = {
        title: {
          text: "折前标准毛利率",
        },
        xAxis: {
          type: "category",
          data: this.xAxisProduct,
          axisLabel: {
            interval: 0,
            formatter: function (value) {
              var ret = ""; //拼接加\n返回的类目项
              var maxLength = 5; //每项显示文字个数
              var valLength = value.length; //X轴类目项的文字个数
              var rowN = Math.ceil(valLength / maxLength); //类目项需要换行的行数
              if (rowN > 1) {
                //如果类目项的文字大于5,
                for (var i = 0; i < rowN; i++) {
                  var temp = ""; //每次截取的字符串
                  var start = i * maxLength; //开始截取的位置
                  var end = start + maxLength; //结束截取的位置
                  //这里也可以加一个是否是最后一行的判断，但是不加也没有影响，那就不加吧
                  temp = value.substring(start, end) + "\n";
                  ret += temp; //凭借最终的字符串
                }
                return ret;
              } else {
                return value;
              }
            },
          },
        },
        yAxis: {
          min: 0, // 设置默认的最小值
          max: 100, // 设置默认的最大值
          type: "value",
          axisLabel: {
            color: "#999",
          },
        },
        tooltip: {
          trigger: "axis", // 触发类型设置为坐标轴轴线触发
          axisPointer: {
            type: "cross", // 设置指示器类型为十字准星指示器
          },
          formatter: function (params) {
            var xValue = params[0].axisValue; // 获取 X 轴的数值
            var yValue = params[0].data; // 获取对应的 Y 轴数值
            return xValue + "  " + " 折前标准毛利率：" + yValue;
          },
        },
        series: [
          {
            name: "category",
            type: "bar",

            data: [],
          },
        ],
      };
      myChart.setOption(option);
    },
    SleectTwo() {
      let chartDom = this.$refs.selectTwo;
      let myChart = this.$echarts.init(chartDom);
      let option;
      option = {
        title: {
          text: "实际毛利率",
        },
        xAxis: {
          type: "category",
          data: this.xAxisProduct,
          axisLabel: {
            interval: 0,
            formatter: function (value) {
              var ret = ""; //拼接加\n返回的类目项
              var maxLength = 5; //每项显示文字个数
              var valLength = value.length; //X轴类目项的文字个数
              var rowN = Math.ceil(valLength / maxLength); //类目项需要换行的行数
              if (rowN > 1) {
                //如果类目项的文字大于5,
                for (var i = 0; i < rowN; i++) {
                  var temp = ""; //每次截取的字符串
                  var start = i * maxLength; //开始截取的位置
                  var end = start + maxLength; //结束截取的位置
                  //这里也可以加一个是否是最后一行的判断，但是不加也没有影响，那就不加吧
                  temp = value.substring(start, end) + "\n";
                  ret += temp; //凭借最终的字符串
                }
                return ret;
              } else {
                return value;
              }
            },
          },
        },
        yAxis: {
          min: 0, // 设置默认的最小值
          max: 100, // 设置默认的最大值
          type: "value",
          axisLabel: {
            color: "#999",
          },
        },
        tooltip: {
          trigger: "axis", // 触发类型设置为坐标轴轴线触发
          axisPointer: {
            type: "cross", // 设置指示器类型为十字准星指示器
          },
          formatter: function (params) {
            var xValue = params[0].axisValue; // 获取 X 轴的数值
            var yValue = params[0].data; // 获取对应的 Y 轴数值
            return xValue + "  " + " 实际毛利率" + yValue;
          },
        },
        series: [
          {
            name: "category",
            type: "bar",
            data: [],
          },
        ],
      };
      myChart.setOption(option);
    },
    handleNodeClick() {},
    filterNode() {},
    createdTimeChange(v) {
      let data = new Date(v);
      let y = data.getFullYear();
      let m = data.getMonth() + 1;
      this.byMonth.pandianData = y + String(m).padStart(2, "0");

      getPanDianCode(this.byMonth).then((res) => {
        if (res.code == 200) {
          this.DropDownList = res.data;
          this.NewDropDownList = [];
          this.DropDownList.forEach((element) => {
            let label =
              new Date(element.startTime).getDate() +
              "号-" +
              new Date(element.endTime).getDate() +
              "号";
            this.NewDropDownList.push({
              value: element.pandianCode,
              label: label,
            });
          });
          if (res.data != []) {
            this.mount = false;
          }
        }
      });
    },
    deptChange(v) {
      // console.log(v);
      if (v) {
        this.mouday = false;
      } else {
        this.mouday = true;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.clas_All {
  margin: 20px;
}
</style>
